package com.semac.client.rpc;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public abstract class RPCCallBackWrapper<C> implements AsyncCallback<C> {
	
	private IsLoadingWidget loadingWidget;
	private boolean showErrors = false;


	public RPCCallBackWrapper(IsLoadingWidget loadingWidget, boolean showErrors) {
		this.loadingWidget = loadingWidget;
		this.showErrors = showErrors;
		this.setWidgetVisibility(true);
	}
	
	@Override
	public void onFailure(Throwable caught) {
		if (this.showErrors)
			Window.alert(caught.getMessage());
		this.setWidgetVisibility(false);
	}

	@Override
	public void onSuccess(C result) {
		this.setWidgetVisibility(false);
		this.gotResult(result);
	}
	
	
	public abstract void gotResult(C result);

	
	private void setWidgetVisibility(boolean visible){
		if (this.loadingWidget != null)
			if (visible)
				this.loadingWidget.showLoading();
			else
				this.loadingWidget.hideLoading();
	}
	
}
